package service.impl.v2;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.v2.ApplyReturnVOMapper;
import dao.v2.MemberCartVOMapper;
import dao.v2.OrderVOMapper;
import dao.v2.TimerVOMapper;
import model.LogOrder;
import model.LogOrderBack;
import model.OrderGoodVO;
import model.OrderTimer;
import model.OrderVO;
import service.v2.OrderCancelService;
import service.v2.PayService;
import service.v2.SplitService;
import service.v2.TimerService;
@Service
public class OrderCancelServiceImpl implements OrderCancelService{
	
	@Autowired
	private SplitService splitService;
	
	@Autowired
	private OrderVOMapper orderVOMapper;
	
	@Autowired
	private TimerService timerService;
	
	@Autowired
	private TimerVOMapper timerVOMapper;
	
	@Autowired
	private MemberCartVOMapper memberCartVOMapper;
	
	@Autowired
	private ApplyReturnVOMapper applyReturnVOMapper;
	
	@Autowired
	private PayService payService;
	
		/*
		 * 未付款时取消订单(单件)
		 * 
		 */
	public void OrderCalcelNoPay(String order_id,String order_timer_id,String ticket_status,String pay_type,String create_by){
	
		//修改订单状态为交易关闭
		int count = orderVOMapper.updOrderState(order_id,"6","1");
		if(count>0){
			//删除定时器
			orderVOMapper.delOrderTimer(order_timer_id);
			//查询该订单中存在的商品
			List<OrderGoodVO> orderGoodList	= orderVOMapper.getOrderGoodByOrderId(order_id);
			//返还库存
			for (int j = 0; j < orderGoodList.size(); j++) {
				orderVOMapper.updStandardNum(orderGoodList.get(j).getGood_standard_id(),orderGoodList.get(j).getNum());
			}
			
			LogOrder log_ord = new LogOrder();
			log_ord.setOrder_id(order_id);
			log_ord.setMember_id(create_by);
			log_ord.setRemark("【订单】申请取消订单");
			//log_ord.setType("7");
			orderVOMapper.InsertLogOrder(log_ord);
			
			if("1".equals(ticket_status)){
				try {
					splitService.payBackUseTicket(order_id,pay_type,create_by,null,null);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	/*
	 * 确认收货(包含7天自动确认收货)
	 * 
	 */
	public void confirmRecieve(String order_id,String order_timer_id,String ticket_status,String order_type){
		//交易关闭
		int count = orderVOMapper.updOrderStateAndRecieveTime(order_id,"5");
		if(count>0){
			//删除计时器
			orderVOMapper.delOrderTimer(order_timer_id);
			//添加库存
			List<OrderGoodVO> order_good = orderVOMapper.getOrderGoodByOrderId(order_id);
			for (int k = 0; k < order_good.size(); k++) {
				 orderVOMapper.updateStoreSellNum(order_good.get(k).getGood_id(),order_good.get(k).getNum());
			}
			int orderBackCount = orderVOMapper.checkOrderBackState(order_id);
			if("0".equals(ticket_status)&&"0".equals(order_type)){
				//判断订单是否退过
				
				if(orderBackCount==0){
					try {
						splitService.settleAward(order_id);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
			else if("1".equals(ticket_status)&&"0".equals(order_type)){
				// 提货券售出日志状态更变结算
				try {
					if(orderBackCount==0){
						splitService.updateTicketLogSellSuccess(order_id);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	/*
	 * 14天未收到退货商品
	 * 
	 */
	public void neverRecieve14Days(String order_id,String order_back_id,String create_by,String time){
		//14天定时器
		OrderTimer timer = timerVOMapper.getOrderTimerByOrderIdAndState(order_id,"2");
		//待收货定时器
		OrderTimer timer_recieve = timerVOMapper.getOrderTimerByOrderIdAndState(order_id,"1");
		//删除14天未收到定时器
		int count = orderVOMapper.delOrderTimer(timer.getId());
		if(timer!=null&&count>0){
			
			//删除原有的  待收货定时器
			int del_count = orderVOMapper.delOrderTimer(timer_recieve.getId());
			if(del_count>0){
				//修改order state
				applyReturnVOMapper.updOrderState(order_id,"4");
				//修改order_backs_state
				applyReturnVOMapper.updOrderBackState(order_back_id,"9");
				
				LogOrderBack log = new LogOrderBack();
				log.setOrder_back_id(order_back_id);
				log.setMember_id(create_by);
				log.setRemark("因未在14天内收到寄回商品,已取消退货申请");
				log.setType("0");
				applyReturnVOMapper.addLogOrderBack(log);
				
				//重新弄一个定时器简直走
				OrderTimer orderTime = new OrderTimer();
				orderTime.setOrder_id(order_id);
				orderTime.setOpen_yn("Y");
				orderTime.setType("1");
				orderTime.setTime(timer_recieve.getTime());
				memberCartVOMapper.addOrderTimer(orderTime);
				
				final String order_time_idt = orderTime.getId();
				final String order_idt = order_id;
				final String pay_timert = timer_recieve.getTime();
				
				 Thread t = new Thread(new Runnable(){  
			            public void run(){  
			            	timerService.orderPayTimer(order_time_idt, order_idt, "", "0", pay_timert, "1");
			            }
			      });  
			       t.start(); 
				}
			}
		}
	
	public List<OrderVO> getAdminDealOrder(String timer_status) {
		return orderVOMapper.getAdminDealOrder(timer_status);
	}
	
	public void updtest() {
		
	}

}
